{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Lesson 1\n",
    "Python Basic, Lesson 1, \n",
    "* v1.0.1, 2016.12 by David Yi   \n",
    "* v1.0.2, 2017.02 modified by Eamon Zhang\n",
    "* v1.0.3, 2018.02 modified by David Yi\n",
    "* v1.0.4, 2018.12 modified by David Yi\n",
    "\n",
    "### 本章内容要点\n",
    "\n",
    "* python 简介\n",
    "* 准备工作\n",
    "    * 使用标准的 python 和 IDLE\n",
    "    * anaconda 介绍\n",
    "    * jupyter 和 notebook介绍\n",
    "    * Pycharm 介绍\n",
    "    * vxcode 介绍\n",
    "* 基本变量概念\n",
    "* print() 和 input() 用法\n",
    "* pycharm 用法介绍\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## python 简介\n",
    "\n",
    "#### 定义\n",
    "下面的定义来自维基百科，https://zh.wikipedia.org/wiki/Python\n",
    "\n",
    "Python（英国发音：/ˈpaɪθən/ 美国发音：/ˈpaɪθɑːn/），是一种广泛使用的高级编程语言，属于通用型编程语言，由吉多·范罗苏姆 创造，第一版发布于 1991 年。可以视之为一种改良 (加入一些其他编程语言的优点，如面向对象) 的 LISP。作为一种解释型语言，Python 的设计哲学强调代码的可读性和简洁的语法（尤其是使用空格缩进划分代码块，而非使用大括号或者关键词）。相比于 C++ 或 Java，Python 让开发者能够用更少的代码表达想法。不管是小型还是大型程序，该语言都试图让程序的结构清晰明了。\n",
    "\n",
    "与 Scheme、Ruby、Perl、Tcl 等动态类型编程语言一样，Python 拥有动态类型系统和垃圾回收功能，能够自动管理内存使用，并且支持多种编程范式，包括面向对象、命令式、函数式和过程式编程。其本身拥有一个巨大而广泛的标准库。\n",
    "\n",
    "Python 虚拟机本身几乎可以在所有的操作系统中运行。Python 的正式解释器CPython是用 C语言编写的、是一个由社区驱动的自由软件，目前由 Python软件基金会管理。\n",
    "\n",
    "#### 创始人\n",
    "Python 的创始人为吉多·范罗苏姆（Guido van Rossum），是一个非常可爱的荷兰人，不像其他很多目前常用的语言的创始者都退隐江湖那样，Python 的创始者至今还是把握着Python 发展的方向。在 2018年，Rossum 宣布不再参与 Python 的发展，而是改成一个小组。\n",
    "\n",
    "#### 版本\n",
    "Python 2.0于2000年10月16日发布，增加了实现完整的垃圾回收，并且支持Unicode。同时，整个开发过程更加透明，社区对开发进度的影响逐渐扩大。目前2系列的最新版本是 2.7.12，2.7版本将在2020年被停止支持。还有不少以前的项目使用 Python 2.x 系列开发并且维护着，不过这几年很多新项目已经都不支持 Python 2.x 了，所以建议大家不要再学习 Python 2.x 了。\n",
    "\n",
    "Python 3.0于2008年12月3日发布，此版不完全兼容之前的Python源代码。目前 Python 版本是 3.7.x 系列，发布于2018年10月。3.7.* 系列相比之前的 3.6.* 系列，新功能增加的频率快了不少，\n",
    "\n",
    "#### Python 能做什么？\n",
    "\n",
    "![](imgs/python_use.jpg)\n",
    "\n",
    "#### Python 的优点和缺点\n",
    "优点 \n",
    "1. 软件质量         \n",
    "注重可读性，被称作“可执行的伪代码”。比一般脚本语言高很多可维护性和可重用性。极简主义的设计思想，尽管实现一个任务可能有很多种方法，往往只有一种方法是显而易见的，明了的解决办法胜于“魔术”般的方法。\n",
    "\n",
    "2. 提高开发者的效率      \n",
    "代码大小只有C++、Java 的1/3-1/5，意味着可以输入、调试和维护更少的代码。 Python 代码是可立即执行代码，无须传统编译语言或静态语言的编译与链接步骤，意味着修改代码直接看到效果，具有快速调整能力。\n",
    "\n",
    "3. 可移植性      \n",
    "大多数 Python 程序不做任何改变即可在各类平台上运行。\n",
    "\n",
    "4. 标准库/第三方库的支持      \n",
    "有众多成熟的功能模块，包括网站开发、数值计算、服务器应用、数据库应用、游戏开发、Web、人工智能等各个方面，特别是最近几年 Python 在人工智能、机器学习、数据统计方面大放异彩，这也归功于一些非常优秀的第三方库。\n",
    "\n",
    "5. 组件集成      \n",
    "轻松与其他应用程序进行通信。可以调用C/C++的库，也可以被C/C++调用，可以和Java集成。\n",
    "\n",
    "6. 简单易学，非常适合编程初学者\n",
    "支持所有现代编程语言的特性，但是也适当做了简化，在学习曲线、程序效果、程序性能上做到了较好的平衡。\n",
    "\n",
    "缺点\n",
    "1. 执行速度不够快（与 C/C++,Java 等相比）        \n",
    "Python 标准实现方式是将源代码的语句转换为字节码，再将字节码解释出来。但没有将代码编译为底层的二进制代码，所以速度比较慢。\n",
    "\n",
    "2. 在服务端还没有非常强大的框架。\n",
    "\n",
    "3. 在很多领域，优秀的第三方库太多，选择困难。\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 设计哲学\n",
    "\n",
    "Python 的设计哲学是“优雅”、“明确”、“简单”。Python 开发者的哲学是“用一种方法，最好是只有一种方法来做一件事”，也因此它和拥有明显个人风格的其他语言很不一样。在设计Python语言时，如果面临多种选择，Python 开发者一般会拒绝花俏的语法，而选择明确没有或者很少有歧义的语法。这些准则被称为“Python格言”。\n",
    "\n",
    "在 Python 解释器内运行 import this可以如下的内容，称之为 Python 之禅意。\n",
    "\n",
    "    The Zen of Python, by Tim Peters\n",
    "\n",
    "    Beautiful is better than ugly.\n",
    "    Explicit is better than implicit.\n",
    "    Simple is better than complex.\n",
    "    Complex is better than complicated.\n",
    "    Flat is better than nested.\n",
    "    Sparse is better than dense.\n",
    "    Readability counts.\n",
    "    Special cases aren't special enough to break the rules.\n",
    "    Although practicality beats purity.\n",
    "    Errors should never pass silently.\n",
    "    Unless explicitly silenced.\n",
    "    In the face of ambiguity, refuse the temptation to guess.\n",
    "    There should be one-- and preferably only one --obvious way to do it.\n",
    "    Although that way may not be obvious at first unless you're Dutch.\n",
    "    Now is better than never.\n",
    "    Although never is often better than *right* now.\n",
    "    If the implementation is hard to explain, it's a bad idea.\n",
    "    If the implementation is easy to explain, it may be a good idea.\n",
    "    Namespaces are one honking great idea -- let's do more of those!\n",
    "\n",
    "#### 提高编程能力的体会\n",
    "\n",
    "* 世界观和人生观\n",
    "* 天赋和勤奋\n",
    "* 阅读\n",
    "* 实践，实践，再实践\n",
    "* 会问，会搜索\n",
    "\n",
    "####  如何运\n",
    "\n",
    "* Python 交互提示模式\n",
    "* Python 自带的IDLE\n",
    "* Microsoft VSCode\n",
    "* Jetbrain Pycharm"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 准备工作\n",
    "\n",
    "* 下载安装 Python 最新版本。\n",
    "\n",
    "* 或者下载安装 anaconda 最新版本。anaconda 是一个强大的、开源的 Python 整体解决方案，包括最新版本的 Python、jupyter等各类工具、精选的 Python 第三方库等。目前 anaconda 已经包含了 Microsoft 开源强大的代码开发工具\n",
    "\n",
    "### 使用标准的 python 和 IDLE\n",
    "\n",
    "首先到 Python 官网 (www.python.org)，找到下载链接，目前 Python 进入到了 3.5+ 系列，不建议再使用 2.x 系列了，还有不少项目习惯用 Python 2.7.x，Python 官方已经明确除了安全补丁以外，不会继续支持 2.7.x 系列，所以如果是初学者，不用任何犹豫的选择 Python 3。\n",
    "\n",
    "目前 python 最新版本是 3.6.4，而3.7 版本也在测试中了。\n",
    "\n",
    "从 Python 官网的 download 链接开始，以 Python 3.5为例，简单介绍一下如何安装。\n",
    "\n",
    "![](imgs/python_install.jpeg)\n",
    "\n",
    "windows 操作系统只需要下载32位版本即可，对于 Python 的学习不会有任何影响，mac 电脑下载对应的最新版本就可以了。\n",
    "\n",
    "以 windows 操作系统举例，下载后，执行安装程序，如下：\n",
    "\n",
    "![](imgs/python_install_1.jpeg)\n",
    "\n",
    "一般情况下不用做任何修改，几分钟就安装好了，如下:\n",
    "\n",
    "![](imgs/python_install_2.jpeg)\n",
    "\n",
    "安装好后，在开始菜单找到 Python 3.5 程序组，看到其中的 IDLE(Python 3.5 32bit)，运行后就会出现下面的 Python 命令行界面：\n",
    "\n",
    "![](imgs/python_install_5.png)\n",
    "\n",
    "可以在这里进行输入 Python 程序，正确的话，会执行。\n",
    "\n",
    "![](imgs/python_install_3.jpeg)\n",
    "\n",
    "不过在这个类似 shell 的环境里面编辑复杂的 python 程序会比较麻烦，所以，可以在 shell 的 File 菜单里面选择第一项 New File，这时候，一个编辑器窗口就出现了。\n",
    "\n",
    "在编辑器里可以编写比较长的程序，选择菜单 Run 里面的 Run Module，就可以运行程序，结果会显示在一个 shell 窗口中，就像下面这样，注意在运行程序前是需要保存程序文件的。\n",
    "\n",
    "![](imgs/python_install_4.jpeg)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "---\n",
    "\n",
    "### anaconda 介绍\n",
    "\n",
    "![](imgs/anaconda.png)\n",
    "\n",
    "对于初学者，Python 的安装也许并不容易，尤其面临 Python 版本的问题，会浪费很多时间，甚至让人放弃的感觉，anaconda 是一种简便的安装方法，可以完美的兼容 Python 2.7 和 Python 3.x，并集成了许多 packages（第三方包），免去配置环境变量的烦恼，\n",
    "\n",
    "我们觉得 anaconda的优势如下：\n",
    "\n",
    "* 集成很多第三方库，省去一一下载的麻烦； \n",
    "* conda 除了可以安装第三方库以外，还可以将 Python 环境作为安装内容的一部分，因此一台电脑上配置多个 Python 开发环境非常容易；\n",
    "* Pycharm、VSCode 等都直接支持 conda 配置；\n",
    "* 自带很多强大工具，最新集成了微软的开发工具，VSCode，并默认安装好了 Python 扩展；\n",
    "\n",
    "anaconda的下载地址：https://www.continuum.io/downloads \n",
    "\n",
    "anaconda 安装过程大致如下：\n",
    "\n",
    "---\n",
    "\n",
    "### Jupyter 介绍\n",
    "\n",
    "![](imgs/jupyter.png)\n",
    "\n",
    "Jupyter Notebook（此前被称为 IPython notebook）是一个交互式笔记本，支持运行 40 多种编程语言。在这里我们将介绍 Jupyter notebook 的主要特性，以及为什么对于希望编写漂亮的交互式文档的人来说是一个强大工具。\n",
    "\n",
    "如果安装了 anaconda，只要执行`jupyter notebook` 就可以启动了 jupyter 的服务端。或者启动 anaconda-navigator 这个图形化导航工具，来执行 Jupyter\n",
    "\n",
    "如果没有安装 anaconda，可以执行  `pip install jupyter` ，来安装 jupyter，其实它也是 python 的一个第三方扩展包。\n",
    "\n",
    "我们的整个教学实践都会在 Jupyter 环境下进行，因此各位会渐渐熟悉起来。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Pycharm 介绍\n",
    "\n",
    "Python 的开发环境大概至少有几十个，一般的学习、调试用 python 自带的 shell、IDLE 即可，用 jupyter notebook 也可以满足很多需求。\n",
    "\n",
    "真正的日常开发还是需要专业的编辑器，推荐 PyCharm，分为商业版本和免费的教育版本，免费版本用在一般的开发已经绰绰有余。\n",
    "\n",
    "![](imgs/pycharm.png)\n",
    "\n",
    "Pycharm 目前最新版本是 2017.3.3，下载链接：https://www.jetbrains.com/pycharm/download/ \n",
    "\n",
    "### vscode 介绍"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 基本变量概念\n",
    "\n",
    "Python 中的变量赋值不需要类型声明。\n",
    "\n",
    "每个变量在内存中创建，都包括变量的标识，名称和数据这些信息。\n",
    "每个变量在使用前都必须赋值，变量赋值以后该变量才会被创建。\n",
    "等号（=）用来给变量赋值。\n",
    "等号（=）运算符左边是一个变量名,等号（=）运算符右边是存储在变量中的值。\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'bool'>\n"
     ]
    }
   ],
   "source": [
    "counter = 100  # 整型变量\n",
    "miles = 1000.0 # 浮点型（小数）\n",
    "name = \"John\"  # 字符串\n",
    "name2 = 'Tom'\n",
    "bool = False  # 布尔值\n",
    "print(type(bool))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1 2 1\n",
      "Hello Hello\n"
     ]
    }
   ],
   "source": [
    "# 多个变量赋值\n",
    "\n",
    "a = b = c = 1\n",
    "b = 2 \n",
    "print(a,b,c)\n",
    "\n",
    "s = s1 = 'Hello'\n",
    "print(s,s1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1 2 3\n"
     ]
    }
   ],
   "source": [
    "# 多个变量赋值\n",
    "\n",
    "a, b, c = 1, 2, 3\n",
    "print(a,b,c)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2 1\n"
     ]
    }
   ],
   "source": [
    "# 交换变量\n",
    "a, b = 1, 2\n",
    "a, b = b, a\n",
    "print(a,b)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 动态语言\n",
    "Python是动态语言，即变量本身的类型不固定\n",
    "\n",
    "与之对应的静态语言（如Java），在定义变量时，必须先指定变量类型，赋值时如不匹配则报错"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 理解Python变量在内存中的表示\n",
    "当我们输入 a = 'ABC' 时，Python解释器做了2件事情：\n",
    "\n",
    "1 在内存中创建了一个'ABC'的字符串；\n",
    "\n",
    "2 在内存中创建了一个名为a的变量，并把它指向'ABC'。\n",
    "\n",
    "也可以把一个变量a赋值给另一个变量b，这个操作实际上是把变量b指向变量a所指向的数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "ABC\n"
     ]
    }
   ],
   "source": [
    "a = 'ABC'\n",
    "b = a\n",
    "a = 'XYZ'\n",
    "print(b)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "---\n",
    "\n",
    "### print() 和 input() 用法\n",
    "\n",
    "print() 用来显示内容，input()用来输入内容，在类似命令行的操作中，这两条语句非常有用。\n",
    "\n",
    "print() 也支持格式化输出，类似于 c 语言中的 printf。\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "100\n",
      "100\n",
      "100 200 300\n"
     ]
    }
   ],
   "source": [
    "print(100)\n",
    "\n",
    "a = 100\n",
    "print(a)\n",
    "\n",
    "print(100, 200, 300)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "sssss\n",
      "hello sss\n"
     ]
    }
   ],
   "source": [
    "name = input('ss')\n",
    "print( 'hello', name) "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "small\n"
     ]
    }
   ],
   "source": [
    "a = 10\n",
    "if a > 10:\n",
    "    print('big')\n",
    "else:\n",
    "    print('small')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0\n",
      "1\n",
      "2\n",
      "3\n",
      "4\n",
      "5\n",
      "6\n",
      "7\n",
      "8\n",
      "9\n",
      "10\n",
      "11\n",
      "12\n"
     ]
    }
   ],
   "source": [
    "for i in range(13):\n",
    "    print(i)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "h\n",
      "e\n",
      "l\n",
      "l\n",
      "o\n"
     ]
    }
   ],
   "source": [
    "for i in 'hello':\n",
    "    print(i)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The length of hello is 5\n"
     ]
    }
   ],
   "source": [
    "# 支持格式化的 print\n",
    "\n",
    "s = 'hello'\n",
    "x = len(s)  \n",
    "print('The length of %s is %d' % (s,x))  "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "hello from English\n",
      "hello from English\n"
     ]
    }
   ],
   "source": [
    "# 更加好，更加 pythonic 的写法：format函数-增强的格式化字符串函数\n",
    "# 关于format的更多细节，参见 https://docs.python.org/3/library/string.html#format-string-syntax\n",
    "# 通过关键字传参\n",
    "print('{greet} from {language}'.format(greet='hello', language='English'))\n",
    "\n",
    "# 通过位置传参\n",
    "print('{0} from {1}'.format('hello', 'English'))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "---\n",
    "\n",
    "print() 格式化输出说明\n",
    "* %字符：标记转换说明符的开始\n",
    "\n",
    "* 转换标志：-表示左对齐；+表示在转换值之前要加上正负号；“”（空白字符）表示正数之前保留空格；0表示转换值若位数不够则用0填充。\n",
    "\n",
    "* 最小字段宽度：转换后的字符串至少应该具有该值指定的宽度。如果是*，则宽度会从值元组中读出。\n",
    "\n",
    "* 点(.)后跟精度值：如果转换的是实数，精度值就表示出现在小数点后的位数。如果转换的是字符串，那么该数字就表示最大字段宽度。如果是*，那么精度将从元组中读出。\n",
    "\n",
    "* 字符串格式化转换类型。\n",
    "\n",
    "转换类型          含义\n",
    "* d,i：带符号的十进制整数\n",
    "* o：不带符号的八进制\n",
    "* u：不带符号的十进制\n",
    "* x：不带符号的十六进制（小写）\n",
    "* X：不带符号的十六进制（大写）\n",
    "* e：科学计数法表示的浮点数（小写）\n",
    "* E：科学计数法表示的浮点数（大写）\n",
    "* f,F：十进制浮点数\n",
    "* g：如果指数大于-4或者小于精度值则和e相同，其他情况和f相同\n",
    "* G：如果指数大于-4或者小于精度值则和E相同，其他情况和F相同\n",
    "* C：单字符（接受整数或者单字符字符串）\n",
    "* r：字符串（使用repr转换任意python对象)\n",
    "* s：字符串（使用str转换任意python对象）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "     3.142\n",
      "pi = 3.142\n",
      "000003.142\n",
      "3.142     \n",
      "+3.141593\n"
     ]
    }
   ],
   "source": [
    "# 格式化 print 举例\n",
    "\n",
    "pi = 3.141592653  \n",
    "\n",
    "print('%10.3f' % pi) #字段宽10，精度3 \n",
    "\n",
    "print(\"pi = %.*f\" % (3,pi)) #用*从后面的元组中读取字段宽度或精度  \n",
    "  \n",
    "print('%010.3f' % pi) #用0填充空白  \n",
    "  \n",
    "print('%-10.3f' % pi) #左对齐  \n",
    "       \n",
    "print('%+f' % pi) #显示正"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "anaconda-cloud": {},
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
